<template>
  <div class="app-container">
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['hardware:mcu-tenant-type:create']">保存租户的设备授权</el-button>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading"  :data="mcuSysTypeListData" ref="multipleTable"  @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="型号" align="center" prop="model"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="通道数" align="center" prop="channelsNumber"/>
      <el-table-column label="厂商" align="center" prop="vendor"/>
      <el-table-column label="版本" align="center" prop="edition"/>
      <el-table-column label="说明" align="center" prop="specification"/>
    </el-table>
  </div>
</template>

<script>
import {getMcuTenantTypeList,createBatchMcuTenantType} from "@/api/hardware/mcuTenantType";
import {getMcuSysTypeList} from "@/api/hardware/mcuSysType";

export default {
  name: "McuTenantType",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // MCU租户分类列表
      list: [],
      dateRangeCreateTime: [],
      // 查询参数
      tenantData: {},
      queryParams: {
        tenantId: -1,
        mcuSysTypeId: null,
        model: null,
        name: null,
        specification: null,
        vendor: null,
        edition: null,
      },
      mcuSysTypeListData: [],
      multipleSelection: []
    };
  },
  created() {
    this.tenantData=this.$route.params.tenantData
    this.queryParams.tenantId=this.tenantData.id
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      // 执行查询
      getMcuTenantTypeList(this.queryParams).then(response => {
        this.list = response.data;

        /** 查询系统ＭＣＵ列表　*/
        this.loading = true;
        getMcuSysTypeList({}).then(response => {
          this.mcuSysTypeListData = response.data;
          /** 处理选中 */
          if (this.list.length>0) {
            this.$nextTick(() => {
              this.list.forEach(it => {
                let item = this.mcuSysTypeListData.find(item => {
                  return item.model === it.model;
                });
                this.$refs.multipleTable.toggleRowSelection(item,true);
              });
            });
          }
          this.loading = false;
        });

      });
    },
    /** 新增按钮操作 */
    handleAdd() {

      if (this.multipleSelection.length<1){
        this.$modal.alertError("授权设备不到为空");
        return;
      }

      this.multipleSelection.forEach(it => {
        it.tenantId= this.tenantData.id
      });

      createBatchMcuTenantType(this.multipleSelection).then(response => {
        this.$modal.msgSuccess("租户设备授权成功");
        this.getList();
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
